package main.java.com.web;

import java.util.*;
import java.math.*;

import main.java.com.entity.Category;
import main.java.com.entity.Dish;
import main.java.com.service.CategoryService;
import main.java.com.service.DishService;
import main.java.com.service.impl.CategoryServiceImpl;
import main.java.com.service.impl.DishServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * AddDishServlet类用于处理菜品添加
 */
@WebServlet("/AddDishServlet")
public class AddDishServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取所有类别信息
        CategoryService categoryService = new CategoryServiceImpl();
        List<Category> categories = categoryService.getAllCategories();
        // 将类别信息设置为请求属性，以便在添加菜品页面中使用
        request.setAttribute("categories", categories);
        // 转发请求到添加菜品页面
        request.getRequestDispatcher("/backend/addDish.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 从请求中获取菜品信息
        String name = request.getParameter("name");
        Long category_id = Long.parseLong(request.getParameter("category_id"));
        String priceStr = request.getParameter("price");
        BigDecimal price = new BigDecimal(priceStr);
        String image = request.getParameter("image");
        String description = request.getParameter("description");
        Integer status = Integer.parseInt(request.getParameter("status"));

        // 生成一个6位随机数作为商品码
        Random random = new Random();
        int code = 100000 + random.nextInt(900000); // 生成100000到999999之间的随机数
        String codeStr = String.valueOf(code);

        // 初始化日期格式，并从请求中获取日期信息
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date create_time = new Date(); // 默认为当前时间
        Date update_time = new Date(); // 默认为当前时间
        Integer sort = 1; // 默认顺序为1
        Long create_user = 1L; // 默认创建人为1
        Long update_user = 1L; // 默认修改人为1
        Integer is_deleted = 0; // 默认未删除

        // 创建Dish对象并设置其属性
        Dish dish = new Dish();
        dish.setName(name);
        dish.setCategory_id(category_id);
        dish.setPrice(price);
        dish.setImage(image);
        dish.setDescription(description);
        dish.setStatus(status);
        dish.setCode(codeStr); // 设置商品码
        dish.setSort(sort);
        dish.setCreate_time(create_time);
        dish.setUpdate_time(update_time);
        dish.setCreate_user(create_user);
        dish.setUpdate_user(update_user);
        dish.setIs_deleted(is_deleted);

        // 创建DishService实例，用于添加菜品
        DishService dishService = new DishServiceImpl();
        dishService.addDish(dish);

        // 设置添加成功的提示信息，将其存放在请求属性中
        request.setAttribute("addSuccess", "菜品添加成功！");
        // 重定向到显示所有菜品数据的页面
        request.getRequestDispatcher("/DishListServlet").forward(request, response);
    }
}
